import InteractiveBase from "../../LevelCommon/InteractiveBase";
import LevelBaseControl from "../../LevelCommon/LevelBaseControl";
import DialogDataConst from "../../../constant/DialogDataConst";
import Sound from "../../../constant/SoundNameConstant";
import { utils } from "../../../utils/CommonUtil";
import EnumData from "../../../Enum/EnumData";

export default class Gate extends InteractiveBase {
    /** 是否已经打开了*/
    public isOpen: boolean = false;
    /** 需要道具*/
    public needPropId: number;
    /** 目标旋转*/
    private targetRotate: Laya.Vector3;

    public Axis:EnumData.Axis;

    public Angle:number;

    private curLevelControl: LevelBaseControl;

    /** 原始旋转角度*/
    private originAngles: Laya.Vector3;

    private mowner: Laya.Sprite3D;

    onStart() {
        this.mowner = this.owner as Laya.Sprite3D;
        this.curLevelControl = window.gameFacade.LevelMng.GetCurLevelControl();
        this.originAngles =  this.mowner.transform.localRotationEuler.clone();

        let temp = this.originAngles.clone();
        switch(this.Axis){
            case EnumData.Axis.X:
                this.targetRotate = new Laya.Vector3(this.Angle,temp.y,temp.z);
                break;
            case EnumData.Axis.Y:
                this.targetRotate = new Laya.Vector3(temp.x,this.Angle,temp.z);
                break;
            case EnumData.Axis.Z:
                this.targetRotate = new Laya.Vector3(temp.x,temp.y,this.Angle);
                break;
        }
        /** 一开始就打开*/
        if (this.isOpen) {
            this.SetOpenDirect();
        }
    }
    protected InteractiveLogic(): boolean {

        if (this.isOpen) {
            this.CallToLevel(this.owner.name + "_In_Gate");
        }
        else {
            /** 判断是否选择了对应的道具*/
            if (this.curLevelControl.IsSelectProp(this.needPropId) || this.needPropId == 0) {
                if (this.needPropId != 0) {
                    /** 使用道具*/
                    this.curLevelControl.UseCurProp();
                }
                /** 开门*/
                this.SetOpen();
            }
            else {
                this.curLevelControl.ShowDialog(DialogDataConst.door_is_locked);
            }
        }
        return true;
    }
    public SetOpen(): void {
        this.CallToLevel(this.owner.name + "_Open_Gate");

        utils.CommonUtils.DORotateTween(this.mowner.transform, this.mowner.transform.localRotationEuler, this.targetRotate, 500,
            null,true,false)
        this.isOpen = true;
        window.gameFacade.SoundMng.playSound(Sound.soundClipArr.OPEN_GATE);
    }
    /** 关门*/
    public SetClose(): void {
        utils.CommonUtils.DORotateTween(this.mowner.transform, this.mowner.transform.localRotationEuler, this.originAngles, 500,
            null, true,false)
        this.isOpen = false;
        /** 关门*/
        window.gameFacade.SoundMng.playSound(Sound.soundClipArr.CLOSE_DOOR);
    }
    /** 直接打开门*/
    public SetOpenDirect(): void {
        this.CallToLevel(this.owner.name + "_Open_Gate");
        (this.owner as Laya.Sprite3D).transform.localRotationEuler = this.targetRotate;
        this.isOpen = true;
    }
    /** 直接关门*/
    public SetCloseDirect(): void {
        (this.owner as Laya.Sprite3D).transform.localRotationEuler = this.originAngles;
        this.isOpen = false;
    }
}